package com.yanqu.road.server.cmd.yrank.user;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.yrank.UserYRank;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuActivity;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = CrossProtocol.C2_CROSS_YRANK_GET_USER_RANK_NUM, desc = "")
public class CrossYRankGetUserRankNumCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        YRankProto.YRankReqMsg reqMsg = YRankProto.YRankReqMsg.parseFrom(packet.getBytes());
        if (reqMsg.getIdKey().equals(String.valueOf(eGamePlayerEventType.BiaoJuExpRank.getValue()))) {
            CrossBiaoJuActivity activity = CrossBiaoJuMgr.getActivity(reqMsg.getActivityId(), reqMsg.getGroupId());
            if (activity != null) {
                activity.sendUserRank(packet.getPlayerId(), serverId);
            }
            return;
        }

        List<UserYRank> rankList = CrossYRankMgr.getSortUserRankList(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey());
        int rankNum = CrossYRankMgr.getUserRankNum(rankList, packet.getPlayerId());

        RankProto.CrossActivityUserRankRespMsg.Builder msg = RankProto.CrossActivityUserRankRespMsg.newBuilder();
        msg.setRank(rankNum);
        UserYRank userYRank = CrossYRankMgr.getUserYRank(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey(), packet.getPlayerId());
        if (userYRank != null) {
            msg.setValue(userYRank.getValue().toString());
            msg.setParam(userYRank.getParam());
        }
        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_CROSS_ACTIVITY_USER_RANK, msg);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
    }

    @Override
    public void sendData() {

    }
}
